/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmArticulo.java
 *
 * Created on 12/07/2010, 09:24:49 PM
 */

package Interfaz;

import Dominio.Articulo.Articulo;
import Dominio.Articulo.ArticuloElaborado;
import Dominio.Articulo.ArticuloSimple;
import Dominio.Articulo.ManejadoraArticulo;
import javax.swing.JOptionPane;


/**
 *
 * @author MARIU
 */
public class frmArticulo extends javax.swing.JFrame {

    private Articulo objA;

    /** Creates new form frmArticulo */
    public frmArticulo() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        rbtnSimple = new javax.swing.JRadioButton();
        rbtnElaborado = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        txtCodigo = new javax.swing.JTextField();
        txtNom = new javax.swing.JTextField();
        txtDes = new javax.swing.JTextField();
        txtMed = new javax.swing.JTextField();
        txtStockMin = new javax.swing.JTextField();
        txtStock = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlistArticulo = new javax.swing.JList();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanel1.setLayout(null);

        buttonGroup1.add(rbtnSimple);
        rbtnSimple.setSelected(true);
        rbtnSimple.setText("SIMPLE");
        rbtnSimple.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnSimpleActionPerformed(evt);
            }
        });
        jPanel1.add(rbtnSimple);
        rbtnSimple.setBounds(10, 10, 61, 23);

        buttonGroup1.add(rbtnElaborado);
        rbtnElaborado.setText("ELABORADO");
        rbtnElaborado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnElaboradoActionPerformed(evt);
            }
        });
        jPanel1.add(rbtnElaborado);
        rbtnElaborado.setBounds(110, 10, 87, 23);

        jLabel1.setText("Codigo:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 50, 40, 14);

        jLabel2.setText("Nombre:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 80, 50, 14);

        jLabel3.setText("Descripcion:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 110, 60, 14);

        jLabel4.setText("Medida:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(10, 130, 50, 14);

        jLabel5.setText("Stock Minimo:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(10, 160, 70, 14);

        jLabel6.setText("Stock:");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(10, 190, 40, 14);

        btnAgregar.setText("AGREGAR");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        jPanel1.add(btnAgregar);
        btnAgregar.setBounds(360, 50, 90, 40);
        jPanel1.add(txtCodigo);
        txtCodigo.setBounds(90, 40, 140, 20);
        jPanel1.add(txtNom);
        txtNom.setBounds(90, 70, 140, 20);
        jPanel1.add(txtDes);
        txtDes.setBounds(90, 100, 140, 20);
        jPanel1.add(txtMed);
        txtMed.setBounds(90, 130, 140, 20);
        jPanel1.add(txtStockMin);
        txtStockMin.setBounds(90, 160, 140, 20);
        jPanel1.add(txtStock);
        txtStock.setBounds(90, 190, 140, 20);

        jScrollPane1.setViewportView(jlistArticulo);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(30, 240, 340, 140);

        btnSalir.setText("SALIR");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        jPanel1.add(btnSalir);
        btnSalir.setBounds(480, 380, 110, 40);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbtnSimpleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnSimpleActionPerformed
        // TODO add your handling code here:
        this.txtStock.setVisible(true);
        this.txtStockMin.setVisible(true);
        this.txtCodigo.setVisible(true);
        this.txtNom.setVisible(true);
        this.txtDes.setVisible(true);
        this.txtMed.setVisible(true);
        this.jLabel5.setVisible(true);
        this.jLabel6.setVisible(true);
}//GEN-LAST:event_rbtnSimpleActionPerformed

    private void rbtnElaboradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnElaboradoActionPerformed
        // TODO add your handling code here:
        this.txtStock.setVisible(false);
        this.txtStockMin.setVisible(false);
        this.txtCodigo.setVisible(true);
        this.txtNom.setVisible(true);
        this.txtDes.setVisible(true);
        this.txtMed.setVisible(true);
        this.jLabel5.setVisible(false);
        this.jLabel6.setVisible(false);

    }//GEN-LAST:event_rbtnElaboradoActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_btnSalirActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
         // TODO add your handling code here:
        //si es simple
        if (this.rbtnSimple.isSelected())
        {
            //si hay algun campo vacio muestra el cartel
            if(this.txtCodigo.getText().length()==0 || this.txtNom.getText().length()==0 || this.txtDes.getText().length()==0 || this.txtStock.getText().length()==0 || this.txtStockMin.getText().length()==0)
            {
                JOptionPane.showMessageDialog(null, "Ingrese todos los datos");
            }
            else
            {
                //sino agrego me creo un objeto articulo simple
                objA= new ArticuloSimple (Integer.parseInt(this.txtCodigo.getText()),this.txtNom.getText(), this.txtDes.getText(), this.txtMed.getText(), Integer.parseInt(this.txtStockMin.getText()), Integer.parseInt(this.txtStock.getText()));
                //Pregunto si existe el articulo usando el metodo agregarArticulo. Si devuelve true puedo agregar.
                if(ManejadoraArticulo.obtenerInstancia().agregarArticulo(objA) == true)
                 {
                   JOptionPane.showMessageDialog(null, "Se ha agregado un Articulo");
                 }
                else
                {
                      JOptionPane.showMessageDialog(null, "Ya existe este Articulo");
                }
            }
        }
        else
        //si es compuesto
        {
            if(this.txtCodigo.getText().length()==0 || this.txtNom.getText().length()==0 || this.txtDes.getText().length()==0)
            {
                JOptionPane.showMessageDialog(null, "Ingrese todos los datos");
            }
            else
            {
                //sino agrego me creo un objeto articulo simple
                objA= new ArticuloElaborado (Integer.parseInt(this.txtCodigo.getText()),this.txtNom.getText(), this.txtDes.getText(), this.txtMed.getText());
                //Pregunto si existe el articulo usando el metodo agregarArticulo de la manejadora. Si devuelve true puedo agregar.
                if(ManejadoraArticulo.obtenerInstancia().agregarArticulo(objA) == true)
                 {
                    //listo
                   this.jlistArticulo.setListData(ManejadoraArticulo.obtenerInstancia().getColArticulo());
                   JOptionPane.showMessageDialog(null, "Se ha agregado un Articulo");
                 }
                else
                {
                    JOptionPane.showMessageDialog(null, "Ya existe este Articulo");
                }
            }
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        this.jlistArticulo.setListData(ManejadoraArticulo.obtenerInstancia().getColArticulo());
    }//GEN-LAST:event_formWindowActivated

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmArticulo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnSalir;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList jlistArticulo;
    private javax.swing.JRadioButton rbtnElaborado;
    private javax.swing.JRadioButton rbtnSimple;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDes;
    private javax.swing.JTextField txtMed;
    private javax.swing.JTextField txtNom;
    private javax.swing.JTextField txtStock;
    private javax.swing.JTextField txtStockMin;
    // End of variables declaration//GEN-END:variables

}
